﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TRAC_Demo
{
    // T.R.A.C.
    // Main Author: Ryan Redburn
    // Date: 10/20/2013

    static class MailCarrier
    {
        #region [ Fields ]

        private static Mail_DBDataContext MailContext;
        private static TRACobjectsDataContext EmpContext;

        #endregion

        #region [ Methods ]

        // Returns a list of possible recepient employees in the format - "LastName, FirstName"
        public static List<string> RecepientList(int employeeID)
        {
            EmpContext = new TRACobjectsDataContext();
            List<string> roster = new List<string>();

            // Uses a LINQ query to find all employees that the user can send mail to
            var employees = from records in EmpContext.Agents
                            where records.EmployeeID != employeeID
                            select records;

            foreach (var employee in employees)
            {
                roster.Add(employee.lName.Trim() + ", " + employee.fName.Trim());
            }

            EmpContext.Dispose();

            return roster;
        }

        // Gets all of the current user's mail and returns the id and message header
        // Every even number in the index is a mail id and the following odd number is the corresponding message header
        public static List<string> GetMail(int employeeID)
        {
            MailContext = new Mail_DBDataContext();
            List<string> userMail = new List<string>();

            // LINQ query that finds all mail associated with the employee currently logged in
            var mail = from records in MailContext.Mails
                       where records.Recepient == employeeID
                       select records;

            // Adds a message header as well as the mail id to the list to be returned
            foreach (var message in mail)
            {
                userMail.Add(message.MailID.ToString() + " - " + FindSender(message.Sender) + " - " + message.Title);
            }

            MailContext.Dispose();

            return userMail;
        }

        // Retrieves a message from the MailDB
        public static List<string> GetMessage(int selectedMessage)
        {
            MailContext = new Mail_DBDataContext();
            List<string> message = new List<string>();

            // Finds the selected message using a Lambda function
            var messageContents = MailContext.Mails.Where(x => x.MailID == selectedMessage).Single();

            // Adds message fields to the list to be returned
            message.Add(FindSender(messageContents.Sender));
            message.Add(messageContents.Title.Trim());
            message.Add(messageContents.Body.Trim());

            MailContext.Dispose();

            return message;
        }

        // Creates a new message and submits it to the MailDB
        public static void SendMessage(string title, string body, string recepient)
        {
            MailContext = new Mail_DBDataContext();

            // Create new Mail object using given information
            Mail newMessage = new Mail()
            {
                Title = title,
                Body = body,
                DateSent = DateTime.Now,
                Sender = CurrentUser.EmpID,
                Recepient = FindRecepient(recepient)
            };

            MailContext.Mails.InsertOnSubmit(newMessage);
            MailContext.SubmitChanges();
            MailContext.Dispose();
        }

        // Deletes a message from the MailDB
        public static void DeleteMessage(int mailID)
        {
            MailContext = new Mail_DBDataContext();

            // Finds the message to be deleted using a Lambda function
            var message = MailContext.Mails.Where(x => x.MailID == mailID).Single();

            MailContext.Mails.DeleteOnSubmit(message);
            MailContext.SubmitChanges();
            MailContext.Dispose();
        }

        #endregion

        #region [ Helper Methods ]

        // Finds the sender of a message using the given employee id and returns their name
        private static string FindSender(int employeeID)
        {
            EmpContext = new TRACobjectsDataContext();

            // Lambda function used to find the emplpyee using their id
            var sender = EmpContext.Employees.Where(x => x.EmployeeID == employeeID).Single();

            string senderName = (sender.lName.Trim() + ", " + sender.fName.Trim());

            EmpContext.Dispose();

            return senderName;
        }

        // Finds the recepient id of a message using the given name
        private static int FindRecepient(string name)
        {
            EmpContext = new TRACobjectsDataContext();

            // Lambda function used to find the emplpyee using their name
            var recepient = EmpContext.Employees.Where(x => (x.lName.Trim() + ", " + x.fName.Trim()) == name).Single();

            EmpContext.Dispose();

            return recepient.EmployeeID;
        }

        #endregion
    }
}
